<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Implements the session class.
 */
class Svc_SessionManager
{
    /**
     * Stores if the session is already started.
     *
     * @var bool
     */
    private $started = false;

    /**
     * Creates the session class.
     *
     * @return Svc_Session
     */
    public function __construct()
    {
    }

    /**
     * Starts the session manager.
     *
     * @return bool
     */
    public function start()
    {
        if ($this->started == false)
        {
            $this->started = @session_start();
        }

        return $this->started;
    }

    /**
     * Destroys the session handler.
     *
     * @return bool
     */
    public function destroy()
    {
        if ($this->started == true)
        {
            $this->started = !@session_destroy();
            unset($_SESSION);

            return !$this->started;
        }

        return false;
    }

    /**
     * put your comment there...
     *
     */
    public function clear()
    {
        $_SESSION = array();
    }

    /**
     * Sets session variable value.
     *
     * @param $name mixed
     * @param $value mixed
     */
    public function __set($name, $value)
    {
        $_SESSION[$name] = $value;
    }

    /**
     * Gets a session variable value.
     *
     * @param mixed $name
     *
     * @return mixed
     */
    public function __get($name)
    {
        if (isset($_SESSION[$name]))
        {
            return $_SESSION[$name];
        }

        return null;
    }

    /**
     * Checks if session a variable is set.
     *
     * @param string $name
     *
     * @return bool
     */
    public function __isset($name)
    {
        return isset($_SESSION[$name]);
    }

    /**
     * Unsets a variable from the session.
     *
     * @param string $name
     *
     * @return void
     */
    public function __unset($name)
    {
        unset($_SESSION[$name]);
    }

}